package com.jiamingZhao.wrapper;

import com.google.common.collect.Lists;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * SqlPara
 * 封装查询使用的 sql 与参数，主要用于 getSqlPara(...) 返回值
 */
@Data
public class SqlPara<T> implements Serializable {
	
	@Serial
	private static final long serialVersionUID = -8586448059592782381L;
	
	String sql;
	List<Object> paraList;
	Class<T> entityClass;
	
	public SqlPara<T> setSql(String sql) {
		this.sql = sql;
		return this;
	}
	
	public SqlPara<T> addPara(Object para) {
		if (paraList == null) {
			paraList = new ArrayList<>();
		}
		paraList.add(para);
		return this;
	}
	

	public Object[] getPara() {
		if (paraList == null || paraList.isEmpty()) {
			return Lists.newArrayList().toArray();
		} else {
			return paraList.toArray(new Object[0]);
		}
	}
	
	public SqlPara<T> clear() {
		sql = null;
		if (paraList != null) {
			paraList.clear();
		}
		return this;
	}
	
	public String toString() {
		return "Sql: " + sql + "\nPara: " + paraList;
	}
}